package com.chang.service.impl;

import com.chang.bean.ProductCategory;
import com.chang.common.crud.BaseServiceImpl;
import com.chang.mapper.ProductCategoryMapper;
import com.chang.service.ProductCategoryService;

import org.springframework.stereotype.Service;

/**
 * @program: erp-api
 * @description:
 * @author: chang
 * @create: 2025-02-26 15h
 **/
@Service
public class ProductCategoryServiceImpl extends BaseServiceImpl<ProductCategory, ProductCategoryMapper>implements ProductCategoryService {

    @Override
    public int save(ProductCategory entity) {
        entity.setCode(getNextCode(entity.getPid()));
        return super.save(entity);
    }

    /**
     * 获取新的编码
     *
     * @param pid
     * @return
     */
    String getNextCode(int pid) {
        String maxCode = null;
        //根据父id查询最大编码
        maxCode = baseMapper.queryMaxCode(pid);
        if (null == maxCode) {
            if (pid == 0) {
                //一级类别
                return "10";
            } else {
                //不存在 查询id=entity.getPid()的编码
                String code = baseMapper.queryCode(pid);
                maxCode = code + "01";
            }
        } else {
            maxCode = Integer.parseInt(maxCode) + 1 + "";
        }
        return maxCode;
    }
}
